package com.company.thread;


import java.util.concurrent.locks.LockSupport;

/**
 * @description: 演示 LockSupport.park()方法让程序阻塞并用 Thread.interrupted()打断
 * @author: ruoan
 * @date: 2020/10/24 17:07
 */
public class ParkDemo {


    public static void main(String[] args) throws InterruptedException {

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    System.out.println("hello1");
                    LockSupport.park();
                    System.out.println("hello2");
                    Thread.interrupted();
                    //try {
                    //    Thread.sleep(2000);
                    //} catch (InterruptedException e) {
                    //    e.printStackTrace();
                    //}
                }
            }
        },"t");
        t.start();



        t.interrupt(); // 把t的打断标记 false->true, 此时 LockSupport.park() 会中断阻塞

        Thread.sleep(3000);
        LockSupport.unpark(t);

    }
}
